/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionnote.modelservice;

import fr.p10.miage.gestionabsence.model.Connection;
import fr.p10.miage.gestionnote.model.Semestre;
import fr.p10.miage.gestionnote.model.Ue;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author gensky
 */
public class SemestreServices extends UtilService{

    private static Connection cx;

    public static List<Ue> getUes(int idFormation,int idSemestre, int idPromotion,int idEtudiant) throws SQLException {
        List<Ue> listeUes = new ArrayList<Ue>();

        String sql = "SELECT *\n"
                + "FROM ue u \n"
                + "WHERE u.idsem = " + idSemestre + "\n"
                + "AND u.idf = "+idFormation+"";

        PreparedStatement pst = cx.getBDD().prepareStatement(sql);

        if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            while (resultat.next()) {
                //Ajouter idP
                listeUes.add(new Ue(
                        resultat.getInt("idue"),
                        resultat.getInt("idf"),
                        idPromotion,
                        idSemestre,
                        resultat.getString("nom"),
                        idEtudiant));
            }
            resultat.close();
        }

        pst.close();
        return listeUes;
    }

    public static List<Ue> getUes(int idFormation,int[] idsousemestre, int idPromotion,int idEtudiant) throws SQLException {
        List<Ue> listeUes = new ArrayList<Ue>();

        String strNumbers = Arrays.toString(idsousemestre);
       // System.out.println("String generated from Arrays.toString method: " + strNumbers);
        strNumbers = strNumbers.replace("[", "").replace("]", "");
       // System.out.println("Final String: " + strNumbers);

        String sql = "SELECT *\n"
                + "FROM ue u \n"
                + "WHERE u.idsem IN(" + strNumbers + ")\n"
                + "AND u.idf = "+idFormation+"";

        PreparedStatement pst = cx.getBDD().prepareStatement(sql);

        if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            while (resultat.next()) {
                listeUes.add(new Ue(
                        resultat.getInt("idue"),
                        resultat.getInt("idf"),
                        idPromotion,
                        0,
                        resultat.getString("nom"),
                        idEtudiant));
            }
            resultat.close();
        }

        pst.close();
        return listeUes;
    }

    public static Semestre getSemestreById() {
        return null;
    }
}
